let router = require('koa-router')();
let Film = require('../models/film.js');
let Order = require('../models/order.js');
router.prefix('/dianshang');

/**
 * 获取推荐的电影
 */
router.get('/getHotFilm', async (ctx, next) => {
  try {
    let films = await Film.find({status: true}).sort({rate: -1}).limit(5).select('doubanId  original_title -_id coverurl');
    ctx.success(films);
  } catch (err) {
    throw err;
  }
});
/**
 * 获取filmlist
 */
router.get('/filmLists', async (ctx, next) => {
  try {
    let obj = {};
    let myFilms = [];
    let orderFilms = [];
    let films = await Film.find({status: true}).limit(60).select('-_id genres doubanId poster rate collect_count wish_count original_title coverurl price');
    if (ctx.userId) {
      let myOrder = await Order.find({userId: ctx.userId, status: 1});
      for (let i = 0; i < myOrder.length; i++) {
        orderFilms = orderFilms.concat(myOrder[i].films);
      }
    }
    myFilms = orderFilms.map(item => item.doubanId);
    for (let i = 0; i < films.length; i++) {
      let item = films[i];
      let genres = item.genres;
      if (myFilms.includes(item.doubanId)) {
        item._doc.isMyFilm = true;
      } else {
        item._doc.isMyFilm = false;
      }
      for (let j = 0; j < genres.length; j++) {
        let sub = genres[j];
        if (!obj[sub]) {
          obj[sub] = [];
        }
        obj[sub].push(item);
      }
    }
    let arr = [];
    for (let key in obj) {
      arr.push({
        type: key,
        lists: obj[key]
      });
    }
    ctx.success(arr);
  } catch (err) {
    throw err;
  }
});
/**
 * 搜索电影
 */
router.get('/searchFilm', async (ctx, next) => {
  try {
    let { keyWord, doubanId } = ctx.query;
    let films = [];

    if (keyWord) {
      films = await Film.find({$or: [{original_title: {$regex: keyWord}}, {aka: {$regex: keyWord}}]}).select('doubanId  poster rate countries wish_count  genres ratings_count collect_count original_title -_id coverurl directors actors price');
    } else if (doubanId) {
      films = await Film.find({doubanId}).select('doubanId   poster rate countries wish_count  genres ratings_count collect_count original_title -_id coverurl directors actors price');
    } else {
      films = await Film.find({}).limit(30).select('doubanId  poster rate countries wish_count  genres ratings_count collect_count original_title -_id coverurl directors actors price');
    }
    let myFilms = [];
    let orderFilms = [];
    if (ctx.userId) {
      let myOrder = await Order.find({userId: ctx.userId, status: 1});
      for (let i = 0; i < myOrder.length; i++) {
        orderFilms = orderFilms.concat(myOrder[i].films);
      }
      myFilms = orderFilms.map(item => item.doubanId);
    }
    films.forEach(item => {
      if (myFilms.includes(item.doubanId)) {
        item._doc.isMyFilm = true;
      } else {
        item._doc.isMyFilm = false;
      }
    });
    ctx.success(films);
  } catch (err) {
    throw err;
  }
});
/**
 * 获取电影详情
 */
router.get('/getFilmDetail', async (ctx, next) => {
  try {
    let {id = '26752088'} = ctx.query;
    let data = await Film.findOne({doubanId: id}).select('-_id');
    ctx.success(data);
  } catch (err) {
    throw err;
  }
});

/**
 * 猜你喜欢
 */
router.get('/getLike', async (ctx, next) => {
  try {
    let arr = await Order.find({userId: ctx.userId}).select('films');
    let brr = [];
    arr.forEach(item => {
      brr = brr.concat(item.films);
    });
    let ids = brr.map(item => (item.doubanId));
    let films = await Film.find({doubanId: {$nin: ids}, status: true}).limit(15);
    ctx.success(films);
  } catch (err) {
    throw err;
  }
});

/**
 * 搜索电影分页
 */
router.get('/searchFilmPage', async (ctx, next) => {
  try {
    let { current = 1, size = 10 } = ctx.query;
    let films = [];

    films = await Film.find({}).skip((+current - 1) * (+size)).limit(+size).select('doubanId  poster rate countries wish_count  genres ratings_count collect_count original_title -_id coverurl directors actors price');
    let myFilms = [];
    let orderFilms = [];
    if (ctx.userId) {
      let myOrder = await Order.find({userId: ctx.userId, status: 1});
      for (let i = 0; i < myOrder.length; i++) {
        orderFilms = orderFilms.concat(myOrder[i].films);
      }
      myFilms = orderFilms.map(item => item.doubanId);
    }
    films.forEach(item => {
      if (myFilms.includes(item.doubanId)) {
        item._doc.isMyFilm = true;
      } else {
        item._doc.isMyFilm = false;
      }
    });
    ctx.success(films);
  } catch (err) {
    throw err;
  }
});

module.exports = router
;
